#include <string>
#include "atomic.h"
#include "message.h"
#include "mainsimu.h"
#include "strutil.h"
#include "../HeadFile/MES_tank_storage_7_model.h"


/**** the Constructor ****/
MES_tank_storage_7_model::MES_tank_storage_7_model( const string &name)
: Atomic( name )
, MES_tank_7_stream_VGO_1( this->addInputPort( "MES_tank_7_stream_VGO_1") )
, MES_tank_7_stream_VGO_out( this->addInputPort( "MES_tank_7_stream_VGO_out") )
, MES_tank_7_stream_VGO_inv( this->addOutputPort( "MES_tank_7_stream_VGO_inv") )
, MES_tank_7_stream_VGO_2( this->addOutputPort( "MES_tank_7_stream_VGO_2") )
{

		
			string tmpval(MainSimulator::Instance().getParameter( this->description(), "MES_tank_7_stream_VGO_inv_val" ));
			MES_tank_7_stream_VGO_inv_val=str2float(tmpval);
		
		
}


/*** the initFunction ***/
Model &MES_tank_storage_7_model::initFunction()
{

		
			count = 0;
	delay = "00:00:00:00";
			count = 0;
			passivate();
			return *this;
		
		
}

/*** the externalFunction ***/
Model &MES_tank_storage_7_model::externalFunction( const ExternalMessage &msg )
{

			
	if( msg.port() == MES_tank_7_stream_VGO_1 ){
		MES_tank_7_stream_VGO_1_flowrate = msg.value();
		count++;
		holdIn( active, delay);
	}

	if( msg.port() == MES_tank_7_stream_VGO_out ){
		MES_tank_7_stream_VGO_out_flowrate = msg.value();
		count++;
		holdIn( active, delay);
	}

	return *this;
		
		
}

/*** the internalFunction ***/
Model &MES_tank_storage_7_model::internalFunction( const InternalMessage &msg )
{

			
	passivate();
	return *this;
		
		
}

/*** the outputFunction ***/
Model &MES_tank_storage_7_model::outputFunction( const InternalMessage &msg )
{

		
	if(count == 2)
	{
		if(MES_tank_7_stream_VGO_out_flowrate > (MES_tank_7_stream_VGO_inv_val + MES_tank_7_stream_VGO_1_flowrate))
			MES_tank_7_stream_VGO_out_flowrate = MES_tank_7_stream_VGO_inv_val + MES_tank_7_stream_VGO_1_flowrate;
		MES_tank_7_stream_VGO_2_flowrate = MES_tank_7_stream_VGO_out_flowrate;
		MES_tank_7_stream_VGO_inv_val = MES_tank_7_stream_VGO_inv_val + MES_tank_7_stream_VGO_1_flowrate - MES_tank_7_stream_VGO_out_flowrate;
		sendOutput( msg.time(), MES_tank_7_stream_VGO_2, MES_tank_7_stream_VGO_2_flowrate);
		sendOutput( msg.time(), MES_tank_7_stream_VGO_inv, MES_tank_7_stream_VGO_inv_val);
		count = 0;
	}
	return *this;
		
		
}

